<?php

 	class Shop_categoryModel extends Model{

 		public $table = 'shop_category';

 		public $validate=array(

 			array('cname','nonull','名称不能为空',2,3),
 			array('cname','maxlen:20','最大长度不能超过20位',2,3),

 			);

 		public function add_category(){
 			if (!$this->create()) {

 				return false;
 			}

 			return $this->add();
 		}

 		public function addsonCategory(){

 			if (!$this->create()) {

 				return false;
 			}

 			return $this->add();
 		}

 		public function edit($cid){

 			if (!$this->create()) {

 				return false;
 			}

 			$this->where("cid={$cid}")->update();

 			return true;
 		}

 		public function get_choose($cid){
 			//获得所有分类
 			$data = $this->all();
 			//获得子集
 			$sonData = $this->get_son($data,$cid);
 			//压人自己
 			$sonData[]=$cid;
 			//将分类的数组用逗号隔开形成字符串
 			// $sonCid = implode(',',$sonData);
 			// $data = $this->where("cid in ({$sonCid})")->all();

 			return $sonData;
 		}

 		//获得子集的id[递归求子集的cid]
 		public function get_son($data,$cid){
 			$temp=array();
 			foreach($data as $v){
 				if ($v['pid'] == $cid) {
 					$temp[]=$v['cid'];
 					$temp = array_merge($temp,$this->get_son($data,$v['cid']));
 				}
 			}
 			return $temp;
 		}

 	} 
























?>